<?php
/**
 * Created by PhpStorm.
 * User: 汪利东
 * Date: 2016/4/8
 * Time: 9:55
 */

namespace Admin\Controller;


use Admin\Common\Page;

class ActivityController extends BaseController
{
    public function index()
    {
        $map = [];
        $keywords = I('get.keywords');
        if (!empty($keywords)) {
            $map['title'] = ['like', "%$keywords%"];
//            $map['_logic'] = 'OR';
        }

        $count = M('Activity')->where($map)->count();// 查询满足要求的总记录数
        $Page = new  Page($count, C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $where['keywords'] = $keywords;
        foreach ($where as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出

        $data = M('Activity')
            ->where($map)
            ->order('activity_id desc')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->data = $data;

        $this->display();
    }

    function export(){
        $data = M('Activity')
            ->order('activity_id desc')
            ->select();

        $file = excel_export($data);
        ouput_Browser($file);
    }

    public function add()
    {
        $this->display('edit');
    }

    public function edit()
    {
        $activity_id = I('get.activity_id');
        $data = M('Activity')->where(['activity_id' => $activity_id])->find();
        $this->data = $data;

        $this->display();
    }

    public function doEdit()
    {
        $activity_id = I('get.activity_id');
        $data = I('post.');
        if($_FILES['a_logo']['name']!='') {
            $info = admin_upload();
            if ($info) {
                $data['a_logo'] = '/uploads/' . $info['a_logo']['savepath'] . $info['a_logo']['savename'];
            }
        }

        if (empty($activity_id)) {
            //新增
            $data['apply_num']=$data['apply_total'];
            $r = M('activity')->add($data);
            if (!empty($r)) {
                $this->success('新增成功', U('index'));
            } else {
                $this->success('新增失败');
            }
        } else {
            //编辑
            $r = M('activity')->where(['activity_id' => $activity_id])->save($data);
            if (!empty($r)) {
                $this->success('修改成功');
            } else {
                $this->success('修改失败');
            }
        }
    }

    public function del()
    {
        $activity_id = I('get.activity_id');
        $r = M('activity')->where(['activity_id' => $activity_id])->delete();
        if ($r) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败，请联系管理员');
        }
    }
}